package com.bh.weapp.controller.customer;

import com.bh.common.annotation.Log;
import com.bh.common.core.controller.BaseController;
import com.bh.common.core.domain.AjaxResult;
import com.bh.common.core.page.TableDataInfo;
import com.bh.common.enums.BusinessType;
import com.bh.extend.domain.JcCustomerUser;
import com.bh.extend.service.IJcCustomerUserService;
import com.bh.order.convert.JcOrderConvert;
import com.bh.order.domain.JcOrder;
import com.bh.order.domain.JcOrderReceivingParty;
import com.bh.order.domain.JcOrderTrack;
import com.bh.order.dto.JcOrderQueryDto;
import com.bh.order.service.IJcOrderReceivingPartyService;
import com.bh.order.service.IJcOrderService;
import com.bh.order.service.IJcOrderTrackService;
import com.bh.order.vo.JcOrderAddVo;
import com.bh.order.vo.JcOrderRowVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 订单Controller
 *
 * @author slx
 * @date 2023-04-22
 */
@Api(tags = {"CUST客户订单API"})
@RestController
@RequestMapping("/api/cust/order")
public class JcOrderCustApiController extends BaseController {

    @Autowired
    private IJcOrderService jcOrderService;
    @Autowired
    private IJcCustomerUserService jcCustomerUserService;
    @Autowired
    private IJcOrderTrackService jcOrderTrackService;
    @Autowired
    private IJcOrderReceivingPartyService orderReceivingPartyService;
    @Autowired
    private JcOrderConvert jcOrderConvert;

    /**
     * 查询订单列表
     */
    @ApiOperation("查询订单列表")
    @PostMapping("/list")
    public TableDataInfo list(@RequestBody JcOrderQueryDto queryVo) {
        JcCustomerUser customerUser = jcCustomerUserService.selectJcCustomerUserByUserId(getUserId());
        List<JcOrderRowVo> list = new ArrayList<>();
        if (customerUser != null) {
            // 查询客户订单
            queryVo.setJcCustomerId(customerUser.getCustomerId());
            queryVo.setStatus(null);
            startPage();
            list = jcOrderService.selectJcOrderList(queryVo);
        } else {
            // 查询收货人订单
            JcOrderReceivingParty orderReceivingParty = new JcOrderReceivingParty();
            orderReceivingParty.setIphone(getLoginUser().getUser().getPhonenumber());
            orderReceivingParty.setType(1);
            List<JcOrderReceivingParty> orderReceivingParties = orderReceivingPartyService.selectJcOrderReceivingpartyList(orderReceivingParty);
            if (orderReceivingParties != null && orderReceivingParties.size() > 0) {
                Set<Long> orderIds = orderReceivingParties.stream().map(JcOrderReceivingParty::getJcOrderId).collect(Collectors.toSet());
                startPage();
                queryVo.setOrderIds(orderIds);
                queryVo.setStatus(null);
                list = jcOrderService.selectCustJcOrderList(queryVo);
            }

        }
        return getDataTable(list);
    }


    /**
     * 新增保存订单
     */
    @Transactional(rollbackFor = Exception.class)
    @ApiOperation("新增保存订单")
    @Log(title = "订单", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult addSave(@RequestBody JcOrderAddVo addVo) {
        JcOrder jcOrder = jcOrderConvert.toDO(addVo);
        JcCustomerUser customerUser = jcCustomerUserService.selectJcCustomerUserByUserId(getUserId());
        if (customerUser == null) {
            return error("未绑定客户！");
        }
        jcOrder.setJcCustomerId(customerUser.getCustomerId());
        // 保存
        int num = jcOrderService.insertJcOrder(jcOrder);
//        if (num > 0 && Objects.equals(user.getUserType(), UserConstants.SYSTEM_USER_TYPE)) {
//            // 推送消息,给站点负责人发短信
//            Map<String, String> map = new HashMap<>();
//            map.put("name", customer.getName());
//            map.put("code", jcOrder.getCode());
//            AsyncManager.me().execute(AsyncFactory.pushSms(user.getPhonenumber(), map, SmsConstants.CACHE_TEMPLATE_CODE_2));
//        }
        return toAjax(num);
    }

    /**
     * 修改订单
     */
    @ApiOperation("获取订单")
    @GetMapping("/getInfo/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        JcOrder jcOrder = jcOrderService.selectJcOrderById(id);
        return AjaxResult.success(jcOrder);
    }

    /**
     * 修改保存订单
     */
    @Transactional(rollbackFor = Exception.class)
    @ApiOperation("修改保存订单")
    @Log(title = "订单", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public AjaxResult editSave(@RequestBody JcOrder jcOrder) {
        JcCustomerUser customerUser = jcCustomerUserService.selectJcCustomerUserByUserId(getUserId());
        jcOrder.setJcCustomerId(customerUser.getCustomerId());
        // 已经进行确认的订单禁止修改
        return toAjax(jcOrderService.updateJcOrder(jcOrder));
    }


    /**
     * 查询订单追踪列表
     */
    @ApiOperation("查询订单追踪列表")
    @GetMapping("/track/{jcOrderId}")
    public AjaxResult list(@PathVariable Long jcOrderId) {
        JcOrderTrack jcOrderTrack = new JcOrderTrack();
        jcOrderTrack.setJcOrderId(jcOrderId);
        List<JcOrderTrack> list = jcOrderTrackService.selectJcOrderTrackList(jcOrderTrack);
        return AjaxResult.success(list);
    }
}
